package com.yanti.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.yanti.domain.ResponseResult;
import com.yanti.domain.dto.SendVerificationCodeReqDTO;
import com.yanti.domain.dto.user.UpdatePasswordReqDTO;
import com.yanti.domain.dto.user.UpdateUserInfoReqDTO;
import com.yanti.domain.dto.user.UserLoginReqDTO;
import com.yanti.domain.enums.AppHttpCodeEnum;
import com.yanti.domain.exception.BizException;
import com.yanti.service.UserService;
import com.yanti.service.VerificationCodeService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.util.HashMap;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {

    @Resource
    private VerificationCodeService verificationCodeService;

    @Resource
    private UserService userService;

    // 登录接口，浏览器访问： URL_ADDRESS
    // 登录接口，浏览器访问： http://localhost:8080/user/doLogin?username=zhang&password=123456
    @RequestMapping("/doLogin")
    public String doLogin(String username, String password) {
        // 此处仅作模拟示例，真实项目需要从数据库中查询数据进行比对
        if("zhang".equals(username) && "123456".equals(password)) {
            StpUtil.login(10001);
            return "登录成功";
        }
        return "登录失败";
    }

    // 查询登录状态，浏览器访问： http://localhost:8080/user/isLogin
    @RequestMapping("/isLogin")
    public String isLogin() {
        return "当前会话是否登录：" + StpUtil.isLogin();
    }

    @PostMapping("/verification/code/send")
    public ResponseResult<?> sendVerificationCode(@Validated @RequestBody SendVerificationCodeReqDTO verificationCodeReqDTO) {
        return verificationCodeService.sendVerificationCode(verificationCodeReqDTO);
    }

//    @PostMapping("/test")
//    public ResponseResult<?> test(@RequestBody @Validated User user, BindingResult bindingResult) {
//        // 主动定义一个运行时异常，分母不能为零
//        int i = 1 / 0;
//        return ResponseResult.okResult();
//    }

    @PostMapping("/test")
    public ResponseResult test() {
        // 手动抛异常，入参是前面定义好的异常码枚举，返参统一交给全局异常处理器搞定
        throw new BizException(AppHttpCodeEnum.SYSTEM_ERROR);
    }

    @PostMapping("/login")
    public ResponseResult<?> loginAndRegister(@Validated @RequestBody UserLoginReqDTO userLoginReqDTO){
        return userService.loginAndRegister(userLoginReqDTO);
    }

    @PostMapping("/logout")
    public ResponseResult<?> logout() {
        return userService.logout();
    }

    @GetMapping("/getUserInfo")
    public ResponseResult<?> getUserInfo() {
        return userService.getUserInfo();
    }

    @PostMapping("/update/password")
    public ResponseResult<?> updatePassword(@Validated @RequestBody UpdatePasswordReqDTO updatePasswordReqDTO) {
        return userService.updatePassword(updatePasswordReqDTO);
    }

    @PostMapping(value = "/update")
    public ResponseResult<?> updateUserInfo(@Validated @RequestBody UpdateUserInfoReqDTO updateUserInfoReqDTO) {
        return userService.updateUserInfo(updateUserInfoReqDTO);
    }
//    @PostMapping("/uploadFiles")
//    public ResponseResult<?> uploadFiles(@RequestParam(value = "avatar", required = false) MultipartFile avatar,
//                                         @RequestParam(value = "backgroundImg", required = false) MultipartFile backgroundImg) {
//        Map<String, String> result = new HashMap<>();
//        if (avatar != null &&!avatar.isEmpty()) {
//            String avatarUrl = userService.uploadFile(avatar, "avatar");
//            result.put("avatarUrl", avatarUrl);
//        }
//        if (backgroundImg != null &&!backgroundImg.isEmpty()) {
//            String backgroundImgUrl = userService.uploadFile(backgroundImg, "backgroundImg");
//            result.put("backgroundImgUrl", backgroundImgUrl);
//        }
//        return ResponseResult.okResult(result);
//    }

    @PostMapping("/uploadFiles")
    public ResponseResult<?> uploadFiles(@RequestParam(value = "avatar", required = false) MultipartFile avatar,
                                                           @RequestParam(value = "backgroundImg", required = false) MultipartFile backgroundImg) {
        Map<String, String> result = new HashMap<>();
        if (avatar != null && !avatar.isEmpty()) {
            String avatarUrl = userService.uploadFile(avatar, "avatar");
            result.put("avatarUrl", avatarUrl);
        }
        if (backgroundImg != null && !backgroundImg.isEmpty()) {
            String backgroundImgUrl = userService.uploadFile(backgroundImg, "backgroundImg");
            result.put("backgroundImgUrl", backgroundImgUrl);
        }
        return ResponseResult.okResult(result);
    }
}